[C#] 寫入檔名替換非法字元


Posted by mike-hsieh on 2023-09-27

紀錄一下替換非法字元的做法。

// 取得非法字元集合
var invalidFileNameChars = Path.GetInvalidFileNameChars();
// Decode
var htmlDecode = Uri.UnescapeDataString(name);
// 比對若有不合法的字元,就替換掉
foreach (var nameChar in invalidFileNameChars)
{
    htmlDecode = htmlDecode.Replace(nameChar, '_');
}

參考來源:

  1. https://dotblogs.com.tw/Null/2020/06/03/140512

#非法字元替換







Related Posts

Leetcode 刷題 pattern - 美國軟體工程師求職有趣經驗

Leetcode 刷題 pattern - 美國軟體工程師求職有趣經驗

[ Nuxt.js 2.x 系列文章 ] Nuxt.js 套件應用-Swiper

[ Nuxt.js 2.x 系列文章 ] Nuxt.js 套件應用-Swiper

Airflow 動手玩:(五)Airflow 設定檔

Airflow 動手玩:(五)Airflow 設定檔


Comments